package karaokit.com;

import android.media.MediaPlayer;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class LyricsSynconizer extends Thread {
	
	private MediaPlayer mPlayer;
	private TextView mView;
	private Lyrics mLyric;
	private Handler handler;	
	private int syncDiff = 0;
	
	public int getSyncDiff() {
		return syncDiff;
	}

	public void setSyncDiff(int syncDiff) {
		this.syncDiff = syncDiff;
	}
	
	public LyricsSynconizer(MediaPlayer mp, Lyrics lyrics, TextView view) {
		mPlayer = mp;
		mView = view;
		mLyric = lyrics;
		handler = new Handler();
	}
	
	@Override
	public void run() {				
		while (mPlayer.isPlaying()) {
			handler.post(sync);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}	
		}		
	}
	
	final Runnable sync = new Runnable() {
        public void run() {
        	Log.e("Runnable updateResults", "Runnable updateResults");
        	mView.setText(mLyric.getLine(mPlayer.getCurrentPosition() + getSyncDiff()));				
        }
   };
   
   @Override
   public void destroy() {	   
	   handler.removeCallbacks(sync);
	   super.destroy();
   }
}
